package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.service.MarketKeywordService;
import com.cskaoyan.market.service.impl.MarketKeywordServiceImpl;
import com.cskaoyan.market.util.HttpUtil2;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/keyword/*")
public class MarketKeywordController extends HttpServlet {

    // 【父接口接收
    private MarketKeywordService marketKeywordService = new MarketKeywordServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        // 选择框
        String keyword = req.getParameter("keyword");
        String url = req.getParameter("url");

        List<MarketKeyword> marketKeywordList = marketKeywordService.list(page, limit, sort, order, keyword, url);

        Object o = ResponseUtil.okList(marketKeywordList);
        // java--->json
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/keyword/", "");
        if ("delete".equals(op)) {
            delete(req, resp);
        } else if ("create".equals(op)) {
            create(req, resp);
        }else if("update".equals(op)){
            update(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String url = JacksonUtil.parseString(requestBody, "url");
        Boolean isHot = JacksonUtil.parseBoolean(requestBody, "isHot");
        Boolean isDefault = JacksonUtil.parseBoolean(requestBody, "isDefault");
        Integer sortOrder = JacksonUtil.parseInteger(requestBody, "sortOrder");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        Boolean deleted = JacksonUtil.parseBoolean(requestBody, "deleted");

        MarketKeyword marketKeyword = marketKeywordService.update(id,keyword,url,isHot,isDefault,sortOrder,updateTime,deleted);

        Object ok = ResponseUtil.ok(marketKeyword);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        String keyword = JacksonUtil.parseString(requestBody, "keyword");
        String url = JacksonUtil.parseString(requestBody, "url");
        Boolean isHot = JacksonUtil.parseBoolean(requestBody, "isHot");
        Boolean isDefault = JacksonUtil.parseBoolean(requestBody, "isDefault");

        // 判空 // 好像前端就已经有判断了
        MarketKeyword marketKeyword = marketKeywordService.create(keyword, url, isHot, isDefault);

        Object ok = ResponseUtil.ok(marketKeyword);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        String id = JacksonUtil.parseString(requestBody, "id");

        int returnCode = marketKeywordService.delete(id);

        // {"errno":0,"errmsg":"成功"}
        if (returnCode == 0) {
            Object ok = ResponseUtil.ok();
            resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
        } else {
            Object fail = ResponseUtil.fail();
            resp.getWriter().println(JacksonUtil.writeValueAsString(fail));
        }
    }
}
